const startsWith = (origin, str) => {
  if (str == null || str == '' || origin.length == 0 || str.length > origin.length)
    return false
  if (origin.substr(0, str.length) == str)
    return true
  return false
}
const array_contains = (array, obj) => {
  for (let i = 0; i < array.length; i++) {
    if (array[i] == obj)//如果要求数据类型也一致，这里可使用恒等号===
      return true
  }
  return false
}
/**
 * 使用时，注意，需要添加 area_*的容器  如<div ref="area_head">  ，mount方法中需要添加 this.refreshDynamicHeight()
 */
export default {
  data () {
    return {
      dynamicHeight: 250
    }
  },
  methods: {
    refreshDynamicHeight (salt, hiddenRefs) {
      this.$nextTick(() => {
        // 盐值默认为98 ，实际为外框高度之和 70 + 28
        salt = salt || 98
        const refs = this.$refs
        let totalHeight = 0
        // let refHeight = 0
        for (let key in refs) {
          let ignore = false
          if (hiddenRefs && hiddenRefs.length) {
            for (let i = 0; i < hiddenRefs.length; i++) {
              if (array_contains(hiddenRefs, key)) {
                ignore = true
                break
              }
            }
          }
          if (ignore)
            continue
          if (startsWith(key, 'area_')) {
            if (refs[key].getBoundingClientRect) {
              totalHeight += refs[key].getBoundingClientRect().height
              if (key === "area_search") {
                totalHeight += 5
              }
            }
          }
          // if (startsWith(key, 'area_')) {
          //   if (refs[key].getBoundingClientRect) {
          //     refHeight = refs[key].getBoundingClientRect().height
          //   }
          //   if ('area_search' === key && refHeight !== 0) {
          //     refHeight += 1
          //   }
          //   totalHeight += refHeight
          // }
        }
        let dHeight = window.innerHeight - totalHeight - salt - 1
        this.dynamicHeight = dHeight > 200 ? dHeight : 200
      })
    }
    //,
    // autoDynamicHeight (hasSubTab) {
    //   let me = this
    //   me.$nextTick(() => {
    //     // tab头高度
    //     let tabHeight = 42
    //     // 子tab头高度
    //     let subTabHeight = 0
    //     if (hasSubTab === true) {
    //       subTabHeight = 38
    //     }
    //     // 底部状态栏高度
    //     let bbarHeight = 28
    //     let refHeight = {
    //       'area_head': 28,        // 麵包屑标签行高度
    //       'area_search': 34,      // 查询条件高度
    //       'area_actions': 30,     // 操作按钮行高度
    //       'area_page': 28         // 分页条高度
    //     }
    //     let tableHeight = window.innerHeight - tabHeight - subTabHeight - bbarHeight
    //     for (let refKey in me.$refs) {
    //       if (refKey === 'area_head') {
    //         tableHeight = tableHeight - refHeight['area_head']
    //       } else if (refKey === 'area_search') {
    //         let searchHeight = me.$refs[refKey].getBoundingClientRect().height
    //         if (searchHeight === 0) {
    //           searchHeight = 1
    //         } else {
    //           searchHeight = searchHeight + 2
    //         }
    //         tableHeight = tableHeight - searchHeight
    //       } else if (refKey === 'area_actions') {
    //         tableHeight = tableHeight - refHeight['area_actions']
    //       } else if (refKey === 'area_page') {
    //         tableHeight = tableHeight - refHeight['area_page']
    //       }
    //     }
    //     tableHeight = tableHeight - (salt - 120)
    //     me.$set(me, 'dynamicHeight', tableHeight)
    //   })
    // }
  }
}
